﻿using System;
using System.Data.SQLite;
using DVDTek_Data_Object.Accessor;

namespace DVDTek_Data_Object.DataObject
{
    public class MovieDataObject
    {
        #region Members
        private int id;
        private string title;
        private int director_id;
        private string origin;
        private int year;
        private bool seen;
        private bool acquired;
        private bool available;
        private string support;

        public int Id
        {
            get { return this.id; }
        }
        public string Title
        {
            get { return this.title; }
            set
            {
                MovieDataObject movie = MovieAccessor.Instance.Select(value);
                if ((movie == null) || (movie.Id == this.Id))
                    this.title = value;
                else if (this.Id == -1)
                {
                    this.id = movie.Id;
                    this.title = value;
                    this.DirectorId = movie.DirectorId;
                    this.Origin = movie.Origin;
                    this.Year = movie.Year;
                    this.Seen = movie.Seen;
                    this.Acquired = movie.Acquired;
                    this.Available = movie.Available;
                    this.Support = movie.Support;
                }
                else
                    throw new SQLiteException("Value is already use by another record");
            }
        }
        public int DirectorId
        {
            get { return this.director_id; }
            set
            {
                if (DirectorAccessor.Instance.Select(value) != null)
                    this.director_id = value;
                else
                    throw new InvalidOperationException(
                        string.Concat("No record in ", DirectorAccessor.TABLE_NAME, " with this id."));
            }
        }
        public string Origin
        {
            get { return this.origin; }
            set { this.origin = value; }
        }
        public int Year
        {
            get { return this.year; }
            set { this.year = value; }
        }
        public bool Seen
        {
            get { return this.seen; }
            set { this.seen = value; }
        }
        public bool Acquired
        {
            get { return this.acquired; }
            set { this.acquired = value; }
        }
        public bool Available
        {
            get { return this.available; }
            set
            {
                if ((this.Acquired) || (value == false))
                    this.available = value;
                else
                    throw new InvalidOperationException("Movie can't be available without been acquired.");
            }
        }
        public string Support
        {
            get { return this.support; }
            set { this.support = value; }
        }
        #endregion

        #region Constructors
        public MovieDataObject()
        {
            this.id = -1;
            setDefaultValue();
            this.Title = string.Empty;
        }
        public MovieDataObject(string title)
        {
            this.id = -1;
            setDefaultValue();
            this.Title = title;
        }
        public MovieDataObject(int id, string title)
        {
            this.id = id;
            setDefaultValue();
            this.Title = title;
        }
        public MovieDataObject(int id, string title, int director_id, string origin, int year,
            bool seen, bool acquired, bool available, string support)
        {
            this.id = id;
            this.title = title;
            this.DirectorId = director_id;
            this.Origin = origin;
            this.Year = year;
            this.Seen = seen;
            this.Acquired = acquired;
            this.Available = available;
            this.Support = support;
        }
        private void setDefaultValue()
        {
            this.DirectorId = -1;
            this.Origin = string.Empty;
            this.Year = -1;
            this.Seen = false;
            this.Acquired = false;
            this.Available = false;
            this.Support = string.Empty;
        }
        #endregion

        public MovieDataObject Copy()
        {
            return new MovieDataObject(this.Id, this.Title);
        }

        public void Update()
        {
            if (this.id == -1)
                this.id = MovieAccessor.Instance.Insert(this.Title, this.DirectorId, this.Origin,
                    this.Year, this.Seen, this.Acquired, this.Available, this.Support);
            else
                MovieAccessor.Instance.Update(this.Id, this.Title, this.DirectorId, this.Origin,
                    this.Year, this.Seen, this.Acquired, this.Available, this.Support);
        }

        public void Delete()
        {
            if (this.id != -1)
            {
                MovieAccessor.Instance.Delete(this.Id);
                this.id = -1;
            }
        }

        public static MovieDataObject GetMovieDataObject(int id)
        {
            return MovieAccessor.Instance.Select(id);
        }
    }
}
